﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YJKRevitView
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdHideBaseline : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException("commandData");
				}
				CmdViewType types = (CmdViewType)127;
				if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					Document document = cmdData.Application.ActiveUIDocument.Document;
					transaction = new Transaction(document);
					transaction.Start("Hide Baseline");
					List<ViewPlan> viewPlanList = this.GetViewPlanList(document);
					HideBaselineForm hideBaselineForm = new HideBaselineForm(this.GetViewPlanInfo(document, viewPlanList));
					if (hideBaselineForm.ShowDialog() != DialogResult.OK)
					{
						if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
						{
							transaction.RollBack();
						}
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						List<int> selectedIndex = hideBaselineForm.SelectedIndex();
						this.SetHideBaseline(viewPlanList, selectedIndex);
						transaction.Commit();
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch (Exception ex)
			{
				msg = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private List<ViewPlan> GetViewPlanList(Autodesk.Revit.DB.Document doc)
		{
			List<ViewPlan> list = new List<ViewPlan>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(ViewPlan)))
			{
				ViewPlan viewPlan = element as ViewPlan;
				if (viewPlan != null && ((int)viewPlan.ViewType == 1 || (int)viewPlan.ViewType == 2 || (int)viewPlan.ViewType == 116))
				{
					ElementId typeId = viewPlan.GetTypeId();
					if (!(null == typeId))
					{
						ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
						if (null != elementId && -1 != elementId.IntegerValue)
						{
							list.Add(viewPlan);
						}
					}
				}
			}
			list.Sort(new ViewPlaneComparer());
			return list;
		}

		private List<string> GetViewPlanInfo(Autodesk.Revit.DB.Document doc, List<ViewPlan> viewPlanList)
		{
			List<string> list = new List<string>();
			foreach (ViewPlan viewPlan in viewPlanList)
			{
				string text = "";
				ElementId typeId = viewPlan.GetTypeId();
				Element element = doc.GetElement(typeId);
				if (element != null)
				{
					ViewFamilyType viewFamilyType = element as ViewFamilyType;
					text = "(" + viewFamilyType.Name + ")";
				}
				ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
				if (null != elementId && -1 != elementId.IntegerValue)
				{
					Element element2 = doc.GetElement(elementId);
					if (element2 != null)
					{
						Level level = element2 as Level;
						if (level != null)
						{
							string name = level.Name;
							string item = string.Concat(new string[]
							{
								viewPlan.Name,
								text,
								"__基线:(",
								name,
								")"
							});
							list.Add(item);
						}
					}
				}
			}
			return list;
		}

		private void SetHideBaseline(List<ViewPlan> viewPlanList, List<int> selectedIndex)
		{
			foreach (int index in selectedIndex)
			{
				ElementId elementId = new ElementId(-1);
				viewPlanList[index].GetUnderLayParameter().Set(elementId);
			}
		}
	}
}
